/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BarrelMoveFrame.java
 *
 * Created on Mar 8, 2011, 4:02:54 PM
 */

package gbbmgmt;

import org.jdesktop.application.Action;
import java.util.*;
import javax.swing.JOptionPane;

/**
 *
 * @author dchapman
 */
public class BarrelMoveFrame extends javax.swing.JFrame {

    public ArrayList<String[]> Vessels;
    public ArrayList<String[]> Beers;

    String BeerCSV = "";
    String BeerText = "";

    /** Creates new form BarrelMoveFrame */
    public BarrelMoveFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jComboBoxVessel = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        jComboBoxBeer = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldVol = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButtonAddBeer = new javax.swing.JButton();
        jButtonSubmit = new javax.swing.JButton();
        jButtonReset = new javax.swing.JButton();
        jButtonExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jDateChooser1.setName("jDateChooser1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gbbmgmt.GBBMGMTApp.class).getContext().getResourceMap(BarrelMoveFrame.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jComboBoxVessel.setName("jComboBoxVessel"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jComboBoxBeer.setName("jComboBoxBeer"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jTextFieldVol.setText(resourceMap.getString("jTextFieldVol.text")); // NOI18N
        jTextFieldVol.setName("jTextFieldVol"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setRows(5);
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gbbmgmt.GBBMGMTApp.class).getContext().getActionMap(BarrelMoveFrame.class, this);
        jButtonAddBeer.setAction(actionMap.get("AddBeer")); // NOI18N
        jButtonAddBeer.setText(resourceMap.getString("jButtonAddBeer.text")); // NOI18N
        jButtonAddBeer.setName("jButtonAddBeer"); // NOI18N

        jButtonSubmit.setAction(actionMap.get("BASubmit")); // NOI18N
        jButtonSubmit.setText(resourceMap.getString("jButtonSubmit.text")); // NOI18N
        jButtonSubmit.setName("jButtonSubmit"); // NOI18N

        jButtonReset.setAction(actionMap.get("BAReset")); // NOI18N
        jButtonReset.setText(resourceMap.getString("jButtonReset.text")); // NOI18N
        jButtonReset.setName("jButtonReset"); // NOI18N

        jButtonExit.setAction(actionMap.get("BAExit")); // NOI18N
        jButtonExit.setText(resourceMap.getString("jButtonExit.text")); // NOI18N
        jButtonExit.setName("jButtonExit"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxVessel, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 545, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxBeer, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldVol, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addComponent(jButtonAddBeer)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonSubmit)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonReset)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 342, Short.MAX_VALUE)
                .addComponent(jButtonExit)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 525, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel1)
                    .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBoxVessel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBoxBeer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldVol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jButtonAddBeer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSubmit)
                    .addComponent(jButtonReset)
                    .addComponent(jButtonExit))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BarrelMoveFrame().setVisible(true);
            }
        });
    }

    public void LoadList(){
        //Reset all data.
        jComboBoxBeer.removeAllItems();
        jComboBoxVessel.removeAllItems();

        //Set Date.
        //Get todays date.
        java.util.Date dt = new java.util.Date();
        //java.sql.Date todaysdate = new java.sql.Date( dt.getTime());
        jDateChooser1.setDate(dt);

        //Fill in vessels and beers.
        DBA.openDBCon();
        Beers = DBA.RunSQLQuery("SELECT BAID,BABrew,BAVol FROM BarrelAging WHERE BAVol>'0'");
        Vessels = DBA.RunSQLQuery("SELECT FVID,FVDesg FROM FV");

        //Fill In Beers.
        for(String s[] : Beers){
            DBA.GetBrewsData(s[1]);
            DBA.GetBeerData(Integer.toString(DBA.BBeerID));
            String Desc = "Batch " + s[1] + " - " + DBA.BName + " - (" + s[2] + " BBL)";
            jComboBoxBeer.addItem(Desc);
        }

        for(String s[] : Vessels){
            jComboBoxVessel.addItem(s[1]);
        }

        DBA.closeDBCon();

        //Reset all variables to blank.
        BeerCSV = "";
        BeerText = "";

        RefreshDisp();

    }

    @Action
    public void AddBeer() {
        DBA.openDBCon();
        String BeerChoice = jComboBoxBeer.getSelectedItem().toString();
        String BeerID = "";
        for(String s[] : Beers){
            DBA.GetBrewsData(s[1]);
            DBA.GetBeerData(Integer.toString(DBA.BBeerID));
            String Desc = "Batch " + s[1] + " - " + DBA.BName + " - (" + s[2] + " BBL)";
            if (Desc.equals(BeerChoice)) {
                BeerID = s[0];
                if(!jTextFieldVol.equals("")){
                    String Volume = jTextFieldVol.getText();
                    BeerCSV += BeerID + "," + Volume + ",";
                    BeerText += "Batch " + s[1] + " - " + DBA.BName + " - (";
                    BeerText += Volume + " BBL)" + "\n";
                }
            }
        }

        //Fill in the beer text.

        DBA.closeDBCon();

        RefreshDisp();
    }

    public void RefreshDisp(){
        //Display text in text area.
        jTextArea1.setText(BeerText);
    }

    @Action
    public void BAReset() {
        int response = JOptionPane.showConfirmDialog(null,
                "This will reset current selections, continue?",
                "Warning",JOptionPane.WARNING_MESSAGE);

        if (response == 0){
            BeerCSV="";
            BeerText="";
            RefreshDisp();
        }
    }

    @Action
    public void BAExit() {
        dispose();
    }

    @Action
    public void BASubmit() {
        int response = JOptionPane.showConfirmDialog(null,
                "This will submit barrel movement, continue?",
                "Warning",JOptionPane.WARNING_MESSAGE);

        if (response == 0){
            //Open Database connection.
            DBA.openDBCon();

            //trim last comma off of BeerCSV
            String NewBeerCSV = BeerCSV.substring(0,BeerCSV.length() - 1);

            //Get Date
            java.util.Date dte = jDateChooser1.getDate();
            java.sql.Date MoveDate = new java.sql.Date( dte.getTime() );

            //Split the string and put into array.
            String[] barry = NewBeerCSV.split(",");
            int count = 0;
            int NumLines = 0;
            float totalVol = 0;

            while (count < barry.length){
                DBA.GetBarrelAgingData(barry[count]);
                float partVol = Float.valueOf(barry[count+1].trim()).floatValue();
                totalVol += partVol;
                float NewVol = DBA.BAVol - partVol;
                if(NewVol < 0) NewVol = 0;

                //Go through CSV and update volumes.
                String query = "UPDATE BarrelAging SET BAVOL='" + Float.toString(NewVol) +
                        "' WHERE BAID='" + barry[count] + "'";
                DBA.RunSQLUpdateQuery(query);

                //Increment count.
                count += 2;
            }

            //Create a movement and put the CSV in the notes Column.
            //Fill in fields.
            String fields = "MovementDate,MovementBrewID,MovementOldState,MovementStateID,MovementVolume" +
                    ",MovementNotes,MovementOVID,MovementNVID";

            String BrewID = "0";
            String OldStateID = "25";
            String NewStateID = "15";
            String OldVessel = "0";

            String NewVessel = "";

            //Get New Vessel Data.
            String VesselChoice = jComboBoxVessel.getSelectedItem().toString();
            for(String s[] : Vessels){
                if (s[1].equals(VesselChoice)) {
                    NewVessel = s[0];
                }
            }

            //Fill in query values.
            String query = "'" +
                MoveDate + "','" +
                BrewID + "','" +
                OldStateID + "','" +
                NewStateID + "','" +
                Float.toString(totalVol) + "','" +
                NewBeerCSV + "','" +
                OldVessel + "','" +
                NewVessel + "'";
            //Insert new data.
            DBA.RunSQLInsertQuery("Movement",fields,query);

            //Close Database Connection.
            DBA.closeDBCon();

            LoadList();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAddBeer;
    private javax.swing.JButton jButtonExit;
    private javax.swing.JButton jButtonReset;
    private javax.swing.JButton jButtonSubmit;
    private javax.swing.JComboBox jComboBoxBeer;
    private javax.swing.JComboBox jComboBoxVessel;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextFieldVol;
    // End of variables declaration//GEN-END:variables

}
